package com.peter.proxy.hkgolden.api;

import java.io.IOException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateDeserializer extends JsonDeserializer<Date> {

	static final Pattern PATTERN = Pattern.compile("\\d+");

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public Date deserialize(JsonParser jp, DeserializationContext ctxt)
			throws IOException, JsonProcessingException {
		final Matcher matcher = PATTERN.matcher(jp.getText());
		if (matcher.find()) {
			final String value = matcher.group();
			try {
				return new Date(Long.valueOf(value));
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
			}
		}
		return null;
	}
}
